
# initialize
library(class)
library(e1071)
source("./fun-ml.R")
source("./config")

# Naive Bayes Classifier
NBC.cl <- naiveBayes(Topic ~ ., r.train, laplace = laplace_smoothing)

# remove NA values from tables
for(i in 1:length(NBC.cl$tables)) {
    NBC.cl$tables[[i]][is.na(NBC.cl$tables[[i]])] <- 1
}

NBC.raw <- predict(NBC.cl, r.test, type = "raw")

# prediction matrix
NBC.raw[NBC.raw > rowMeans(NBC.raw)] <- 1
NBC.raw[NBC.raw <= rowMeans(NBC.raw)] <- 0

# create true classes matrix
r.test.cl <- NBC.raw
r.test.cl[,] <- 0

for (i in 1:(dim(r.test)[1])) {
    r.test.cl[i, r.test.lab[[i]]] <- 1
}

# count measures
NBC.ta <- TA(r.test.cl, NBC.raw)
NBC.spe <- specificity(r.test.cl, NBC.raw)
NBC.sen <- sensitivity(r.test.cl, NBC.raw)
NBC.fm <- Fmeasure(r.test.cl, NBC.raw)

